package com.sugon.task;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.NumberUtil;
import com.sugon.dao.MesShiftprojectDao;
import com.sugon.dao.MesStoplineDao;
import com.sugon.entity.KpiMtbfEntity;
import com.sugon.entity.KpiMttrEntity;
import com.sugon.modules.kpi.model.ShiftAccuStatVO;
import com.sugon.modules.kpi.model.StopLineStatVO;
import com.sugon.modules.utils.DateUtil;
import com.sugon.service.KpiMtbfService;
import com.sugon.service.KpiMttrService;
import com.sugon.service.SysDeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * KPI统计任务,每次执行，统计前一天的KPI
 * 主要为MES提供数据
 * 统计OEE、设备开动率、MTTR、MTBF
 *
 * @author YuXD
 * @since 2020-07-18 09:36
 */
@Slf4j
@Component("kpiStatTask")
public class KpiStatTask {

    @Autowired
    private SysDeptService deptService;
    @Autowired
    private MesStoplineDao stoplineDao;
    @Autowired
    private KpiMttrService kpiMttrService;
    @Autowired
    private KpiMtbfService kpiMtbfService;
    @Autowired
    private MesShiftprojectDao shiftprojectDao;

    public void process() {
        // 获取昨天日期
        String yesterday = DateUtil.yesterday();
        // 获取昨日停线统计数据
        List<StopLineStatVO> yesterdayStopLineStat = stoplineDao.doStopLineStat(yesterday);

        // 计算MTTR
        statMttrKpi(yesterday, yesterdayStopLineStat);

       statMtbfKpi(yesterday, yesterdayStopLineStat);


    }

    /**
     * 统计MTTR(故障平均恢复时间) KPI
     * 计算公式：停线时间/停线次数
     *
     * @param statDay      统计日期,即统计的是哪天的数据
     * @param stopLineStatList 停线统计数据
     */
    private void statMttrKpi(String statDay, List<StopLineStatVO> stopLineStatList) {
        KpiMttrEntity kpiMttr;
        for (StopLineStatVO stopLineStatVO : stopLineStatList) {
            kpiMttr = new KpiMttrEntity(stopLineStatVO.getLineCode());
            kpiMttr.setStatDate(cn.hutool.core.date.DateUtil.parseDate(statDay));
            kpiMttr.setMttr(NumberUtil.div(stopLineStatVO.getMinuteTotal(),stopLineStatVO.getTimes(),0));
            kpiMttrService.save(kpiMttr);
        }

    }

    /**
     * 统计MTBF KPI
     * 计算公式：（实际生产时间（产线排班时间）-停线时间）/停线次数
     */
    private void statMtbfKpi(String statDay, List<StopLineStatVO> stopLineStatList) {
        // 获取产线轮班累计时长统计
        Map<String, ShiftAccuStatVO> lineShiftAccuDataStat = getLineShiftAccuDataStat(statDay);
        KpiMtbfEntity kpiMtbf;
        ShiftAccuStatVO shiftAccuStat;
        String lineCode;
        double hourTotal;
        for (StopLineStatVO stopLineStat : stopLineStatList) {
            hourTotal = 0.0;
            lineCode = stopLineStat.getLineCode();
            kpiMtbf = new KpiMtbfEntity(lineCode);
            kpiMtbf.setStatDate(cn.hutool.core.date.DateUtil.parseDate(statDay));
            // 设置产线稼动时长
            shiftAccuStat = lineShiftAccuDataStat.get(lineCode);
            if (shiftAccuStat != null) {
                hourTotal = shiftAccuStat.getHourTotal();
            }
            // 计算并设置产线MTBF
            kpiMtbf.setMtbf((hourTotal - NumberUtil.div(stopLineStat.getMinuteTotal().doubleValue(),60.0,0))/stopLineStat.getTimes());
            kpiMtbfService.save(kpiMtbf);
        }
    }

    /**
     * 获取指定日期的停线统计数据
     * 返回以产线CODE为key,统计数据为value的Map
     *
     * @param day 日期，格式为yyyy-MM-dd
     * @return
     */
    private Map<String, ShiftAccuStatVO> getLineShiftAccuDataStat(String day) {
        Map<String, ShiftAccuStatVO> lineShiftStat = new HashMap<>();
        List<ShiftAccuStatVO> lineShiftAcctStatList = shiftprojectDao.doLineShiftAccuStat(day);
        if (CollectionUtil.isNotEmpty(lineShiftAcctStatList)) {
            lineShiftAcctStatList.forEach(shiftAccuStatVO -> {
                lineShiftStat.put(shiftAccuStatVO.getLineCode(), shiftAccuStatVO);
            });
        }
        return lineShiftStat;
    }

}
